package com.huo.importdata.service;

import com.huo.annotation.TaskLogAnnotation;
import com.huo.common.enumtype.FtpFileStatusEnum;
import com.huo.filestransport.dao.FtpFileInfoDao;
import com.huo.filestransport.entity.FtpFileInfoEntity;
import com.huo.filestransport.vo.FtpServerInfo;
import com.huo.importdata.dao.EsfmMapper;
import com.huo.importdata.entity.Esfm;
import com.huo.util.DateUtils;
import org.quartz.DisallowConcurrentExecution;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.*;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@DisallowConcurrentExecution
public class ImportDataEsfmService {

    private final Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private FtpFileInfoDao ftpFileInfoDao;
    @Autowired
    private EsfmMapper esfmMapper;
    @Autowired
    private FtpServerInfo ftpServerInfo;


    private final static int insertBatchNum = 30;

    @TaskLogAnnotation
    public void importEsfm(){
        String dataDate = DateUtils.DateCount(ftpServerInfo.getDelayTime(),"yyyy-MM-dd");
        importEsfm(dataDate);
    }

    @TaskLogAnnotation
    public void importEsfm(String dataDate){
        logger.info("导入esfm数据开始" );
        try {
            FtpFileInfoEntity record = new FtpFileInfoEntity();
            record.setFtpfilestatus(FtpFileStatusEnum.FTPFIEL_STATUS_UNZIP.getCode());
            record.setFtpfilename("S01_ESFM21.zip");
            record.setDataDateStr(dataDate);
            FtpFileInfoEntity ffe = ftpFileInfoDao.selectBySelective(record);
            if(ffe != null) {
                //将表中数据删除
                esfmMapper.truncateTableEsfm();
                logger.info("导入S01_ESFM21(消费)数据开始" );
                String unzipfilePaht = ffe.getUnzipfilepath();
                long startT = System.currentTimeMillis();
                BufferedInputStream fis = null;
                BufferedReader reader = null;
                try {
                    long start = System.currentTimeMillis();
                    File file = new File(unzipfilePaht);
                    fis = new BufferedInputStream(new FileInputStream(file));
                    reader = new BufferedReader(new InputStreamReader(fis, "utf-8"), 20 * 1024 * 1024);
                    String line = "";
                    int dataNum = 0;
                    //插入次数
                    int insertNum = 0;
                    List<Esfm> list = new ArrayList<>();
                    while ((line = reader.readLine()) != null) {
                        String[] str = line.split("\u001B");
                        //System.out.println(Arrays.toString(str));
                        Esfm esfm = new Esfm();
                        esfm.setX21txcom(str[0]);
                        esfm.setX21trdt(str[1]);
                        esfm.setX21trtm(str[2]);
                        esfm.setX21fsjg(str[3]);
                        esfm.setX21upseq(str[4]);
                        esfm.setX21qsdt(str[5]);
                        esfm.setX21acno(str[6]);
                        esfm.setX21actyp(str[7]);
                        esfm.setX21jsjg(str[8]);
                        esfm.setX21shno(str[9]);
                        esfm.setX21dcfg(str[10]);
                        esfm.setX21amnt(new BigDecimal(str[11]));
                        esfm.setX21bale(new BigDecimal(str[12]));
                        esfm.setX21amnt1(new BigDecimal(str[13]));
                        esfm.setX21fee(new BigDecimal(str[14]));
                        esfm.setX21fee1(new BigDecimal(str[15]));
                        esfm.setX21rcvfee(new BigDecimal(str[16]));
                        esfm.setX21payfee(new BigDecimal(str[17]));
                        esfm.setX21ccy(str[18]);
                        esfm.setX21matyc(str[19]);
                        esfm.setX21txcd(str[20]);
                        esfm.setX21exdt(str[21]);
                        esfm.setX21shty(str[22]);
                        esfm.setX21fwfsm(str[23]);
                        esfm.setX21fwtjm(str[24]);
                        esfm.setX21fwpnm(str[25]);
                        esfm.setX21devid(str[26]);
                        esfm.setX21txorg(str[27]);
                        esfm.setX21refno(str[28]);
                        esfm.setX21auno(str[29]);
                        esfm.setX21esseq(str[30]);
                        esfm.setX21zlsh(str[31]);
                        esfm.setX21date(str[32]);
                        esfm.setX21time(str[33]);
                        esfm.setX21otxcom(str[34]);
                        esfm.setX21otrdt(str[35]);
                        esfm.setX21otrim(str[36]);
                        esfm.setX21ofsjg(str[37]);
                        esfm.setX21oupsq(str[38]);
                        esfm.setX21msgid(str[39]);
                        esfm.setX21admsg(str[40]);
                        esfm.setX21fjdta(str[41]);
                        esfm.setX21dzfg(str[42]);
                        esfm.setX21txflg(str[43]);
                        esfm.setX21devtp(str[44]);
                        esfm.setX21sndfg(str[45]);
                        esfm.setX21avafg(str[46]);
                        esfm.setX21txtyp(str[47]);
                        esfm.setX21rmks(str[48]);
                        esfm.setX21flag(str[49]);
                        esfm.setX21unto(str[50]);
                        esfm.setX21opnod(str[51]);
                        esfm.setX21oper(str[52]);
                        esfm.setX21auth(str[53]);
                        list.add(esfm);
                        if (list.size() == insertBatchNum) {
                            esfmMapper.insertBatch(list);
                            list.clear();
                            insertNum++;
                        }
                        dataNum++;
                    }
                    if (dataNum != insertNum * insertBatchNum) {
                        esfmMapper.insertBatch(list);
                    }
                    logger.info("导入S01_ESFM21数据{}",dataNum);
                    long end = System.currentTimeMillis();
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    try {
                        reader.close();
                        fis.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                long endT = System.currentTimeMillis();
                logger.info("导入S01_ESFM21(消费)数据结束 ，导入数据时间：{}" ,endT - startT);
                FtpFileInfoEntity param = new FtpFileInfoEntity();
                param.setId(ffe.getId());
                param.setFtpfilestatus(FtpFileStatusEnum.FTPFIEL_STATUS_IMPORT.getCode());
                param.setUpdatetime(new Date());
                ftpFileInfoDao.updateByPrimaryKeySelective(param);
            }else {
                logger.info("S01_ESFM21.zip 文件不存在");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        logger.info("导入esfm数据结束" );
    }

}
